package com.wxy.xr.servlet;



import com.wxy.xr.bean.Education;
import com.wxy.xr.service.UserService;
import com.wxy.xr.service.WebsiteService;
import com.wxy.xr.service.impl.UserServiceImpl;
import com.wxy.xr.service.impl.WebsiteServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/education/*")
public class EducationServlet extends BaseServlet<Education> {
    private UserService userService=new UserServiceImpl();
    private WebsiteService websiteService=new WebsiteServiceImpl();
    public void front(HttpServletRequest request, HttpServletResponse response)throws Exception{
        request.setAttribute("educations",service.list());
        request.setAttribute("user",userService.list().get(0));
        request.setAttribute("footer",websiteService.list().get(0).getFooter());
        forward(request, response,"front/education.jsp");
    }
    public  void admin(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setAttribute("educations",service.list());
        forward(request, response,"admin/education.jsp");
    }

    public  void save(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Education education=new Education();
        BeanUtils.populate(education,request.getParameterMap());
        if(service.save(education)){
            redirect(request, response,"education/admin");
        } else {
            forward(request, response,"教育信息保存失败");
           }
    }

    public  void remove(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String[] idstrs=request.getParameterValues("id");
        List<Integer> ids=new ArrayList<>();
        for(String idstr:idstrs){
            ids.add(Integer.valueOf(idstr));
        }

        if(service.remove(ids)){
            redirect(request, response,"education/admin");
        }else{
            forward(request, response,"教育信息删除失败");}
    }
}
